package com.wan.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View

//为网格布局设置间隔距离
class ProjectGridItemDecoration(var context: Context) : RecyclerView.ItemDecoration() {

    private var horDistance: Int = 0
    private var verDistance: Int = 0

    private var rect: Rect = Rect()

    fun setHorizontalDistance(distance: Int) {
        horDistance = distance
    }

    fun setVerticalDistance(distance: Int) {
        verDistance = distance
    }

    private val paint: Paint = Paint()

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            paint.color = context.resources.getColor(android.R.color.transparent, null)
        } else {
            paint.color = context.resources.getColor(android.R.color.transparent)
        }
    }

    override fun onDraw(c: Canvas?, parent: RecyclerView?, state: RecyclerView.State?) {
        super.onDraw(c, parent, state)
        c!!.save()
        drawHorizontal(c, parent)

        drawVertical(c, parent)
        c!!.restore()
    }

    //横向排列之间的 间隔
    private fun drawHorizontal(c: Canvas, parent: RecyclerView?) {
        var childCount = parent!!.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val left = child.left - params.leftMargin;
            val right = child.right + params.rightMargin + verDistance
            val top = child.bottom + params.bottomMargin;
            val bottom = top + horDistance
            rect.set(left, top, right, bottom);
            c.drawRect(rect, paint)
        }

    }

    private fun drawVertical(c: Canvas, parent: RecyclerView?) {

        var childCount = parent!!.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams

            val top = child.top - params.topMargin;
            val bottom = child.bottom + params.bottomMargin;
            val left = child.right + params.rightMargin;
            val right = left + horDistance


            rect.set(left, top, right, bottom);
            c.drawRect(rect, paint)
        }

    }

    override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
        var spanCount = getSpanCount(parent)
        var itemCount = parent!!.adapter.itemCount
        if (isLastRaw(parent, view, spanCount, itemCount)) {
            outRect!!.set(0, 0, 0, verDistance)
        } else if (isLastColumn(parent, view, spanCount)) {
            outRect!!.set(0, 0, horDistance, 0)
        } else {
            outRect!!.set(0, 0, horDistance, verDistance)
        }

    }

    private fun getSpanCount(parent: RecyclerView?): Int {

        var layoutManager = parent!!.layoutManager
        if (layoutManager is GridLayoutManager) {
            return layoutManager.spanCount
        }
        return 0
    }

    //是不是最后一行  不需要绘制右边
    private fun isLastRaw(parent: RecyclerView?, view: View?, spanCount: Int, itemCount: Int): Boolean {
        var position = parent!!.getChildAdapterPosition(view)
        var i = itemCount - itemCount % spanCount
        return position >= i

    }

    //是不是最后一列  不需要绘制右边
    private fun isLastColumn(parent: RecyclerView?, view: View?, spanCount: Int): Boolean {
        var position = parent!!.getChildAdapterPosition(view)
        return (position + 1) % spanCount == 0
    }

}